package com.bookstore.dao.impl;

import java.util.HashSet;
import java.util.Set;

import com.bookstore.dao.TradeDAO;
import com.bookstore.domain.Trade;

public class TradeDAOImpl extends BaseDAO<Trade> implements TradeDAO {
	/**
	 * 向数据表中插入 Trade 对象
	 * @param trade
	 */
	@Override
	public void insert(Trade trade) {
		String sql = "INSERT INTO trade(userId, tradeTime) VALUES (?,?)";
		/*update(sql, trade.getUserId(), trade.getTradeTime());*/
		long tradeId = insert(sql, trade.getUserId(), trade.getTradeTime());
		trade.setTradeId((int) tradeId);
	}
	/**
	 * 根据 userId 获取和其关联的 Trade 的集合
	 * @param userId
	 * @return
	 */
	@Override
	public Set<Trade> getTradesWithUserId(Integer userId) {
		String sql = "SELECT tradeId, userId, tradeTime FROM trade WHERE userId = ?";
		return new HashSet<Trade>(queryForList(sql, userId));
	}

}
